/**
* This file is part of dev.clipall.
*
* dev.clipall is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* You should have received a copy of the GNU General Public License
* along with dev.clipall.  If not, see <http://www.gnu.org/licenses/>.
*
* File      : CategoryForm.java
* Created   : Nov 7, 2009, 10:41:32 AM
*/

package dev.clipall.view;

import dev.clipall.Constants;
import dev.clipall.business.GenericMediator;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

/**
 *
 * @author Erol Hira
 */
public class CategoryForm extends javax.swing.JFrame {

    /** Creates new form CategoryForm */
    public CategoryForm() {
        JPanel contentPanel = new JPanel();
        setContentPane(contentPanel);
        initComponents();
        setLocationRelativeTo(SearchFrame.getInstance());
        try {
            setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource(Constants.Resources.SYSTEM_TRAY_ICON)));
        } catch(Exception ex){}

        contentPanel.registerKeyboardAction(new EscapeKeyActionListener(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JPanel.WHEN_IN_FOCUSED_WINDOW);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextCategoryName = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("New Category");
        setAlwaysOnTop(true);
        setResizable(false);

        jLabel1.setText("Category Name");

        jTextCategoryName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextCategoryNameKeyPressed(evt);
            }
        });

        jButton1.setText("Create");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextCategoryName, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(27, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextCategoryName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(16, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        String categoryName = jTextCategoryName.getText();
        GenericMediator.getInstance().createNewCategory(categoryName);

        hideCategoryForm();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTextCategoryNameKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextCategoryNameKeyPressed
        if(KeyEvent.VK_ENTER == evt.getKeyCode()){
            String categoryName = jTextCategoryName.getText();
            GenericMediator.getInstance().createNewCategory(categoryName);

            hideCategoryForm();
        }
    }//GEN-LAST:event_jTextCategoryNameKeyPressed

    public static void displayNewInstance(){
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

                CategoryForm categoryForm = new CategoryForm();
                categoryForm.setVisible(true);
                categoryForm.setFocusToTextField();
            }
        });
    }
        
    public void hideCategoryForm(){

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                dispose();
            }
        });
    }

    public void setFocusToTextField(){
        jTextCategoryName.requestFocusInWindow();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextCategoryName;
    // End of variables declaration//GEN-END:variables

    class EscapeKeyActionListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            dispose();
        }

    }
}
